package com.service.impl;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.beans.Admin;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.beans.Message;
import com.mapper.MessageMapper;
import com.service.MessageService;
import org.springframework.util.StringUtils;

@Service
@Transactional
public class MessageServiceImpl implements MessageService {
    @Resource
    private MessageMapper messageMapper;

    public List<Message> getMessage(String name) {
        return messageMapper.getMessage(name);
    }

    @Override
    public Map<String, Object> queryByPage(Message message, Integer page, Integer limit) {
        Map<String, Object> resMap = new HashMap<>(4);
        PageHelper.offsetPage((page - 1) * limit, limit);
        List<Message> dataList = Collections.emptyList();
        if(StringUtils.hasText(message.getName())){
            dataList = messageMapper.getMessage(message.getName());
        }else{
            dataList = messageMapper.getAllMessage();
        }
        PageInfo<Message> pageInfo = new PageInfo<>(dataList);
        resMap.put("code", 0);
        resMap.put("msg", "查询成功");
        resMap.put("data", pageInfo.getList());
        resMap.put("count", pageInfo.getTotal());
        return resMap;
    }

    public int addMessage(Message Message) {
        return messageMapper.addMessage(Message);
    }

    public int deleteMessage(int id) {
        return messageMapper.deleteMessage(id);
    }

    public Message getMessageById(int id) {
        return messageMapper.getMessageById(id);
    }

    public List<Message> getAllMessage() {
        return messageMapper.getAllMessage();
    }

    public int updateMessage(Message Message) {
        return messageMapper.updateMessage(Message);
    }
}


